package com.changxianggu.crawler.cloud.test;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.codec.Base64;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import com.changxianggu.cloud.crawler.core.WebApplication;
import com.changxianggu.cloud.crawler.core.modules.storage.entity.CxgBook;
import com.changxianggu.cloud.crawler.core.modules.storage.service.ICxgBookService;
import com.changxianggu.cloud.crawler.core.utils.HttpUtil;
import com.changxianggu.cloud.crawler.core.utils.UserAgentUtil;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = {WebApplication.class})// 指定启动类
@Slf4j
public class NongYeShe {

    @Autowired
    ICxgBookService bookService;

    public static final String COOKIE = "ShopCart=\"\"; _ZMemberName=liuyuantao1990; _ZMemberID=15770; UniqueID=aZ30EjfxDiVWCrCi1582268660615; Sites=_46; JSESSIONID=3F1520A7E986AA4805A5189D80C2E084; _ZVING_AUTHCODE=9560737647f6aa570e80e5798b11fb0d_1582271938682; 46_vq=17";
    public static final String ORIGIN = "https://wx.ccapedu.com";
    public static final String REFERER = "https://wx.ccapedu.com/mtp/goods/list?SiteID=46";
    public static final String URL_PRE = "https://wx.ccapedu.com";


    @Test
    public void initCrawler() throws Exception {
        doCrawler();
    }

    public void doCrawler() throws Exception {
        int pageNum = 1;
        int totalPage = 241;
        int pageSize = 10;
        for (int i = pageNum; i <= totalPage; i++) {
            String formatUrl = "https://wx.ccapedu.com/mtp/goods/list?SiteID=46";
            Map<String, Object> parameter = MapUtil.newHashMap(6);
            parameter.put("PageIndex", i);
            Map<String, Object> headers = MapUtil.newHashMap(5);
            headers.put("Cookie", COOKIE);
            headers.put("User-Agent", UserAgentUtil.random());
            headers.put("Origin", ORIGIN);
            headers.put("Referer", REFERER);
            String result = HttpUtil.doPost(formatUrl, headers, parameter);
            int tryCount = 1;
            while (result.contains("页面没有找到") && tryCount <= 5) {
                //睡眠2秒继续访问
                Thread.sleep(2000);
                result = HttpUtil.doPost(formatUrl, headers, parameter);
                tryCount++;
            }
            if (StrUtil.isNotBlank(result)) {
                Document htmlDoc = Jsoup.parse(result);
                Elements bookContentList = htmlDoc.select(".books-list .books-list-item");
                if (bookContentList != null && bookContentList.size() > 0) {
                    List<CxgBook> listBook = new ArrayList<>();
                    for (Element element : bookContentList) {
                        CxgBook bookEntity = new CxgBook();
                        Element titleEle = element.select(".title a").first();
                        String href = "";
                        String name = "";
                        String pkId = "";
                        if (titleEle != null) {
                            name = titleEle.text();
                            href = titleEle.attr("href");
                            pkId = href.split("&ID=")[1];
                        }
                        CxgBook bookEntity1 = getBookEntity(href);
                        BeanUtil.copyProperties(bookEntity1, bookEntity);
                        bookEntity.setBookName(name);
                        bookEntity.setPkId(pkId);
                        Element priceEle = element.select(".price").first();
                        if (priceEle != null) {
                            String numbers = getNumbers(priceEle.text());
                            if (StrUtil.isNotBlank(numbers)) {
                                bookEntity.setPrice(BigDecimal.valueOf(Double.parseDouble(numbers)));
                            }
                        }
                        Element authorEle = element.select(".author").first();
                        if (authorEle != null) {
                            String text = authorEle.text();
                            bookEntity.setAuthor(text.substring(3));
                        }
                        Element timeEle = element.select(".time").first();
                        if (timeEle != null) {
                            String text = timeEle.text();
                            String substring = text.substring(5);
                            if (StrUtil.isNotBlank(substring)) {
                                substring = substring + "-01 00:00:00";
                                DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
                                LocalDateTime localDateTime = LocalDateTime.parse(substring, dtf);
                                bookEntity.setPubDate(localDateTime);
                            }
                        }
                        bookEntity.setIsSync(0);
                        bookEntity.setCreateTime(LocalDateTime.now());
                        bookEntity.setPressName("中国农业出版社");
                        bookEntity.setStatus(1);
                        bookEntity.setPressId(12);
                        log.info(bookEntity.getBookName());
                        listBook.add(bookEntity);
                    }
                    bookService.saveBatch(listBook);
                    log.info("第" + i + "页数据保存成功");
                    log.warn("农业社--->" + i);
                }
            }
        }
    }

    public CxgBook getBookEntity(String bUrl) throws Exception {
        CxgBook bookEntity = null;
        if (StrUtil.isBlank(bUrl)) {
            return null;
        }
        String url = "http://dlib.cabplink.com/book/{}.shtml";
        Map<String, Object> headers = MapUtil.newHashMap(5);
        headers.put("Cookie", COOKIE);
        headers.put("User-Agent", UserAgentUtil.random());
        headers.put("Origin", ORIGIN);
        headers.put("Referer", REFERER);
        String result = HttpUtil.doGet(bUrl, headers);
        int tryCount = 1;
        while (result.contains("页面没有找到") && tryCount <= 5) {
            //睡眠2秒继续访问
            Thread.sleep(2000);
            result = HttpUtil.doGet(bUrl, headers);
            tryCount++;
        }
        if (StrUtil.isNotBlank(result)) {
            Document htmlDoc = Jsoup.parse(result);
            bookEntity = new CxgBook();
            Elements imgEles = htmlDoc.select(".books-content .goods-cover img");
            if (CollUtil.isNotEmpty(imgEles)) {
                bookEntity.setCover(imgEles.get(0).attr("src"));
            }
            Elements infoEles = htmlDoc.select(".books-content .goods-info .goods-info-t .sub-info-box");
            if (CollUtil.isNotEmpty(infoEles)) {
                for (Element ele : infoEles) {
                    String text = ele.text();
                    if (StrUtil.isNotBlank(text)) {
                        if (text.startsWith("作者：")) {
                            String substring = text.substring(3);
                            if (StrUtil.isNotBlank(substring)) {
                                bookEntity.setAuthor(substring);
                            }
                        } else if (text.startsWith("ISBN：")) {
                            String substring = text.substring(5);
                            if (StrUtil.isNotBlank(substring)) {
                                bookEntity.setBookIsbn(substring.replaceAll("-", "").replaceAll("—", ""));
                            }
                        } else if (text.startsWith("出版时间：")) {
                            String pubStr = text.substring(5);
                            if (StrUtil.isNotBlank(pubStr)) {
                                pubStr = pubStr + "-01 00:00:00";
                                DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
                                LocalDateTime localDateTime = LocalDateTime.parse(pubStr, dtf);
                                bookEntity.setPubDate(localDateTime);
                            }
                        } else if (text.startsWith("丛书：")) {
                            String awardsStr = text.substring(3);
                            bookEntity.setAwards(awardsStr);
                        }

                    }
                }
            }
            Elements btnEles = htmlDoc.select(".books-content .goods-info .goods-info-b .big-btn button");
            if (CollUtil.isNotEmpty(btnEles)) {
                for (Element ele : btnEles) {
                    String text = ele.text();
                    if (StrUtil.isNotBlank(text) && text.contains("在线试读")) {
                        String onclick = ele.attr("onclick");
                        if (StrUtil.isNotBlank(onclick)) {
                            String substring = onclick.substring(14, onclick.length() - 2);
                            if (substring.contains("&")) {
                                substring = substring.substring(0, substring.indexOf("&"));
                            }
                            String pdfStr = URL_PRE + "/" + substring;
                            bookEntity.setDigitalUri("https://wx.ccapedu.com/pdf/web/viewer.html?file=" + Base64.encode(pdfStr));
                        }
                    }
                }
            }
            Elements detailEles = htmlDoc.select(".books-content .books-detail .detail-box-con .box-con");
            if (CollUtil.isNotEmpty(detailEles) && detailEles.size() > 2) {
                bookEntity.setCatalog(detailEles.get(0).text());
                bookEntity.setBrief(detailEles.get(1).text());
            }
        }
        return bookEntity;
    }

    /**
     * 判断一个字符串是否都为数字
     *
     * @param strNum
     * @return
     */
    public boolean isDigit(String strNum) {
        Pattern pattern = Pattern.compile("[0-9]{1,}");
        Matcher matcher = pattern.matcher((CharSequence) strNum);
        return matcher.matches();
    }

    /**
     * 截取数字
     *
     * @param content
     * @return
     */
    public String getNumbers(String content) {
        Pattern pattern = Pattern.compile("\\d+");
        Matcher matcher = pattern.matcher(content);
        while (matcher.find()) {
            return matcher.group(0);
        }
        return "";
    }
}
